package fr.esgi.pages;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.esgi.classes.Groupe;
import fr.esgi.classes.Livrable;
import fr.esgi.donnees.Database;
import fr.esgi.utils.Repertoire;

/**
 * Servlet implementation class CreateRepertoire
 */
public class CreateRepertoire extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CreateRepertoire() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String projet=request.getParameter("projet");
		Database d=new Database();
		ArrayList <Livrable> livrables = null;
		ArrayList<Groupe>groupes=null;
		try {
			livrables=d.recuperationLivrable(projet);
			groupes=d.afficherEquipe(projet);
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i=0;i<livrables.size();i++){
			Repertoire  r= new Repertoire(projet, String.valueOf(livrables.get(i).getIdLivrable()));
			r.createRepertoire();
			for(int j=0;j<groupes.size();j++){
			r.createRepertoireGroupe(projet, String.valueOf(livrables.get(i).getIdLivrable()),String.valueOf(groupes.get(j).getId()));	
			}

		}
		getServletContext().getRequestDispatcher("/jsp/pages/accueilConnexion.jsp").forward(request, response);
	}

}
